package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.card.service.CardService;
import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.entity.*;
import com.stlm2.dems.service.*;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Author：fuchunyu
 * Date：2017/10/13
 * Description：
 */
@Controller
@RequestMapping("/userCardsC")
public class UserCardsController {

    @Resource
    private UserCardsService userCardsService;
    @Resource
    private UserService userService;
    @Resource
    private CardsService cardsService;
    @Resource
    private DdmGoodsService ddmGoodsService;
    @Resource
    private GoodsCategoryService goodsCategoryService;


    @ResponseBody
    @RequestMapping(value = "searchUserCardsNum.json",produces = "application/json;charset=UTF-8")
    public String searchUserCardsNum(String companyCode,ModelMap model,String token){
        User user=userService.getByToken(token);
        if (user==null)
            return CodeContent.returnLogin();

        UserCards userCards=new UserCards();
        userCards.setUserId(user.getId());
        userCards.setCardsState(CardsState.Unused);
        List<UserCards> unused=userCardsService.getList(-1,-1,userCards);
        userCards.setCardsState(CardsState.Used);
        List<UserCards> used=userCardsService.getList(-1,-1,userCards);
        userCards.setCardsState(CardsState.Due);
        List<UserCards> due=userCardsService.getList(-1,-1,userCards);
        model.put("unused",unused.size());
        model.put("used",used.size());
        model.put("due",due.size());
        return CodeContent.returnSuccess(model);
    }

    //用户卡券查询
    @ResponseBody
    @RequestMapping(value = "/searchCards.json",produces = "application/json;charset=UTF-8")
    public String searchCards(String companyCode,ModelMap model, String token, String page,Integer cardsStateValue){
        User user=userService.getByToken(token);
        if (user==null)
            return CodeContent.returnLogin();
        PageView pageview =new  PageView();
        int pageNo = 1;
        int pageSize=20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        UserCards userCards=new UserCards();
        userCards.setUserId(user.getId());
        userCards.setCardsStateValue(cardsStateValue);
        pageview= userCardsService.getPage(pageNo,pageSize,userCards);

        if (pageview!=null){
            model.put("data",userCardsService.convert(pageview.getRecords()));
            model.put("recordsTotal", pageview.getRowCount());
            model.put("totalPage", pageview.getPageCount());

        }
        model.put("pageNo", page);
        return CodeContent.returnSuccess(model);
    }

    //用户获取卡券
    @ResponseBody
    @RequestMapping(value = "/getCards.json",produces="application/json;charset=UTF-8")
    public String getCards(String companyCode,String token,String cardsId) throws ParseException {

        User user=userService.getByToken(token);
        if (user==null)
            return CodeContent.returnLogin();
        if(!NumberUtils.isNumber(cardsId))
            return CodeContent.returnFailure("卡券不能为空！");
        Cards cards=cardsService.findEntityById(Integer.valueOf(cardsId));
        if(cards==null)
            return CodeContent.returnFailure("找不到卡券！");
        UserCards userCards=new UserCards();
        userCards.setCardsId(cards.getId());
        userCards.setUserId(user.getId());
        List<UserCards> userCard=userCardsService.getList(-1,-1,userCards);
        if(userCard.size()>0)
            return CodeContent.returnFailure("您已经领过该卡券！");
        userCards.setCardsState(CardsState.Unused);
        userCards.setGetTime(new Date());
        if(cards.getValidDay()!=null){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DAY_OF_MONTH, cards.getValidDay());
            userCards.setEndUseTime(c.getTime());
        }
        String name=cards.getName();

        userCards.setCardsType(cards.getCardsType());
        userCards.setMoney(cards.getMoney());
        userCards.setValidDay(cards.getValidDay());
        if(cards.getGoodsId()!=null) {
            userCards.setGoodsId(cards.getGoodsId());
            DdmGoods goods=ddmGoodsService.findEntityById(cards.getGoodsId());
            if (goods!=null)
                name=goods.getName();
        }
        if(cards.getGoodsCategoryId()!=null) {
            userCards.setGoodsCategoryId(cards.getGoodsCategoryId());
            GoodsCategory goodsCategory=goodsCategoryService.findEntityById(cards.getGoodsCategoryId());
            if (goodsCategory!=null)
                name=goodsCategory.getName()+"类";
        }
        userCards.setName(name);
        userCardsService.addEntity(userCards);
        return CodeContent.returnSuccess(null);
    }

     /*sfy
     * 2017年10月30日10:36:35
     * 用户可用与不可用的卡券列表
     *
     * */



}
